<?php

/**
 * acao actions.
 *
 * @package    camaras
 * @subpackage acao
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class acaoActions extends sfActions
{
  public function executeProvidencias(sfWebRequest $request)
  {
	$this->providencia_acao_list = ($this->getUser()->getAttribute('gerente',null,'usr')) ?
	      Doctrine::getTable('ProvidenciaAcao')
    	    ->createQuery('a')
	        ->execute()
		:
		  Doctrine::getTable('ProvidenciaAcao')->getProvidenciasSecretario($this->getUser()->getAttribute('cd_usuario',null,'usr'));
  }
  
  public function executeFinaliza(sfWebRequest $request)
  {
	$q = Doctrine_Query::create()
	  ->update('Acao a')
	  ->set('a.dt_conclusao', '?', date("Y.m.d H:i:s",time()))
	  ->where('a.cd_acao = ?', $request->getParameter('cd_acao'))
	  ->execute();

	//Log de finalização de Ação
	$hist = new ProvidenciaAcao();
	$hist->cd_acao = $request->getParameter('cd_acao');
	$hist->cd_usuario = $this->getUser()->getAttribute('cd_usuario',null,'usr');
	$hist->de_providencia = 'ação finalizada';
	$hist->save();

	$this->getUser()->setFlash('notice', 'Ação finalizada');
	$this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
  }

  public function executeSetProvidencia(sfWebRequest $request)
  {
  $this->form = new ProvidenciaAcaoForm();

	if ($request->isMethod('POST'))
	{
	  $this->form->bind($request->getParameter('providencia'));
		 
	  if ($this->form->isValid()) 
	  {
	    //echo "<br>É válido";
		$arr_providencia = $_POST['providencia'];
		$descricao = ($arr_providencia['descricao']);

		//echo "<BR>usr: ".$usr."<br>pwd: ".$pwd."<br>pwd md5: ".md5($pwd);
		$hist = new ProvidenciaAcao();
		$hist->cd_acao = $this->getUser()->getAttribute('acao_id',null,'camara');
		$hist->cd_usuario = $this->getUser()->getAttribute('cd_usuario',null,'usr');
		$hist->de_providencia = $descricao;
		$hist->save();
	
		$this->getUser()->setFlash('notice', 'Provindêcia registrada');
		$this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
					  
	  } else {
	  	  $this->getUser()->setFlash('error', 'providência em branco.');			
		  $this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
	  }
	}
  }

  public function executeIndex(sfWebRequest $request)
  {

    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
    $acoes = Doctrine::getTable('Acao')->getAcoesPorCamara($this->getUser()->getAttribute('id',null,'camara'));

    $this->pager = new sfDoctrinePager(
		'Acao',
		sfConfig::get('app_qtd_registros_por_pagina')
	);

    $this->pager->setQuery($acoes);
	$this->pager->setPage($request->getParameter('page', 1));
	$this->pager->init();
		  
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->acao = Doctrine::getTable('Acao')->find(array($request->getParameter('cd_acao')));
	$this->forward404Unless($this->acao);
	$this->providencias = Doctrine::getTable('ProvidenciaAcao')->getPorAcao($request->getParameter('cd_acao'));
	
	$tipo_usuario = $this->getUser()->getAttribute('tipo_usuario');
	$this->gerente = $this->getUser()->getAttribute('gerente',null,'usr');
	
	$this->form = new ProvidenciaAcaoForm();

	$this->getUser()->setAttribute('last_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');
	$this->getUser()->setAttribute('acao_id', $request->getParameter('cd_acao'), 'camara');

  }

  public function executeNew(sfWebRequest $request)
  {
	$q = Doctrine::getTable('Proposicao')->getProposicoesPorCamara($this->getUser()->getAttribute('id',null,'camara'));
	$this->proposicao_list = $q->execute();
			
    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
	$this->form = new AcaoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	$this->proposicao_list = Doctrine::getTable('Proposicao')->getProposicoesPorCamara($this->getUser()->getAttribute('id',null,'camara'));
    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));    $this->forward404Unless($request->isMethod('post'));

    $this->form = new AcaoForm();

    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->proposicao_list = Doctrine::getTable('Proposicao')->getProposicoesPorCamara($this->getUser()->getAttribute('id',null,'camara'));
    $this->acao = Doctrine::getTable('Acao')->find(array($request->getParameter('cd_acao')));
    $this->forward404Unless($acao = Doctrine::getTable('Acao')->find(array($request->getParameter('cd_acao'))), sprintf('Object acao does not exist (%s).', array($request->getParameter('cd_acao'))));
    $this->form = new AcaoForm($acao);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($acao = Doctrine::getTable('Acao')->find(array($request->getParameter('cd_acao'))), sprintf('Object acao does not exist (%s).', array($request->getParameter('cd_acao'))));
    $this->form = new AcaoForm($acao);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($acao = Doctrine::getTable('Acao')->find(array($request->getParameter('cd_acao'))), sprintf('Object acao does not exist (%s).', array($request->getParameter('cd_acao'))));
    $acao->delete();

    $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
	$this->redirect($this->getUser()->getAttribute('show_acoes_page',null,'usr'));
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
	
    if ($form->isValid())
    {
      $acao = $form->save();

      $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
	  $this->redirect($this->getUser()->getAttribute('last_page',null,'usr'));
    }
  }
}
